package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dto.ChannelDTO;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dto.PageResponseResult;
import com.heima.model.common.dto.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    @Override
    public ResponseResult findByQueue(ChannelDTO dto) {
        //1.校验业务参数
        //检查分页参数
        dto.checkParam();
        //2.执行分页条件查询
        //2.1分页条件对象
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.2查询条件
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        //  判断前端是否传入name 如果有值  添加模糊查询条件
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(AdChannel::getName, dto.getName());
        }
        //  判断前端是否传入status 如果有值  添加eq条件
        if (dto.getStatus() != null) {
            wrapper.eq(AdChannel::getStatus, dto.getStatus());
        }
        //  添加排序条件
        wrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> result = this.page(pageReq, wrapper);
        //3. 封装通用分页返回结果

        return new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal(), result.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        String name = channel.getName();
        // 1 参数校验

        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }
        // 2 判断该频道是否存在
        if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能大于10个字符");
        }

        // 3 执行新增
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已存在");
        }
        // 保存到数据库
        channel.setCreatedTime(new Date());
        this.save(channel);
        // 4 返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1 参数校验
        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id为空");
        }
        // 2 判断该频道是否存在
        AdChannel oldChhannel = this.getById(adChannel.getId());
        String newName = adChannel.getName();
        if (StringUtils.isNotBlank(newName) && !newName.equals(oldChhannel.getName())) {
            if (newName.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能大于10个字符");
            }
        }


        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, newName));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已存在");
        }
        this.updateById(adChannel);
        //4 返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "id不能为空");
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = this.getById(id);
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "对应的频道信息不存在");
        }
        // 启用状态下不能删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW, "频道有效,不允许删除");
        }
        //3.删除频道
        removeById(id);
        // 制造异常
     /*if(id < 100){  // 如果id小于100 认为是业务错误
        throw  new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW,"id不能小于100");*/
        //throw new RuntimeException("id不能小于100");
        //return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"id不能小于100");
        //CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"id不能小于100");
        // 返回结果
        return ResponseResult.okResult();
    }
}







